-
Notifications
You must be signed in to change notification settings - Fork 350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add and pass more regression tests for PerseusItem parser #1952
Conversation
Note to reviewers: it's probably going to be easiest to read this one commit at a time. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is coming along nicely Ben! :)
One thing I noticed while reviewing. Do we have the concept of a tuple parser? I noticed some fields are definitely a [number, number]
but we're parsing them as array(number)
.
Example from the matrix-widget.ts parser:
matrixBoardSize: array(number),
object({type: constant("ok")}), | ||
object({type: constant("ok"), value: number}), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This confused me until I went and looked at the implementation and docs. I guess without having a "shape" that represents the discriminant, we'd have to rebuild how TypeScript decides what it is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I agree that this is ugly. For comparison, here's how Zod does it:
const myUnion = z.discriminatedUnion("status", [
z.object({ status: z.literal("success"), data: z.string() }),
z.object({ status: z.literal("failed"), error: z.instanceof(Error) }),
]);
I suppose we could do something like:
function discriminatedUnion<
DiscriminantKey extends keyof any,
T extends {[d: DiscriminantKey]: string | number | boolean}
>(discriminantKey: DiscriminantKey, parseVariant: Parser<T>): DiscriminatedUnionBuilder {
// ...
}
That would mirror how TypeScript thinks about discriminated unions. But the problem is, it wouldn't handle our versioned widget options. Those aren't (in TS's view) valid discriminated unions, since the discriminant, version.major
, is a nested field. My current parser design allows us to treat them kind of like discriminated unions anyway, by splitting the choice of variant and the parsing of that variant into separate steps that can run arbitrary parsing logic.
If only version
were simply a number (like it should have been all along), we could just do the thing that makes intuitive sense here. The curse of pre-TS code strikes again! :/
...and now, after writing all that out, I am tempted to create a special parser abstraction just for versioned widget options. Then we could simplify the one for discriminated unions to be more like Zod's API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like:
const parseMyWidget = parseVersionedWidgetOptions(parseVersion2)
.migratedFrom(parseVersion1, upgradeFromVersion1)
.migratedFrom(parseVersion0, upgradeFromVersion0)
.parser
const input = {type: "ok", value: "foobar"}; | ||
|
||
expect(parse(input, unionParser)).toEqual( | ||
failure(`At (root).value -- expected number, but got "foobar"`), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be useful to include the discriminant info here? Otherwise we don't know which union entry has bad data. 🤔
At (root {type: "ok"}).value -- expected number, but got "footer"
Maybe that gets too verbose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that would be better. The parser context currently doesn't have a way to track which variant of a union you're currently trying to parse, so I can't think of a way to add this info to the message easily.
I'll try adding a forVariant
method to ParseContext, parallel to forSubtree
. Maybe that will be easier than I'm imagining.
const input = {type: "triangle", width: -1, radius: 99}; | ||
|
||
expect(parse(input, unionParser)).toEqual( | ||
failure(`At (root).type -- expected "rectangle", but got "triangle"`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be nice of this could mention all eligible types.
// TODO(benchristel): simplify should never be `true`, but we | ||
// have some content where it is anyway. If we ever backfill | ||
// the data, we should simplify `simplify`. | ||
simplify: optional(nullable(pipeParsers(union(string).or(constant(true)).parser).then(convert(String)).parser)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind adding this note and an example to this doc? https://khanacademy.atlassian.net/wiki/spaces/ENG/pages/3524690040/Content+Issues+for+Backfill
@@ -35,7 +35,10 @@ export const parseNumericInputWidget: Parser<NumericInputWidget> = parseWidget( | |||
answers: array( | |||
object({ | |||
message: string, | |||
value: optional(number), | |||
// TODO(benchristel): value should never be null or undefined, | |||
// but we have some content where it is anyway. If we backfill |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
026af5c
to
c76c65f
Compare
npm Snapshot: PublishedGood news!! We've packaged up the latest commit from this PR (efdc0ff) and published it to npm. You Example: yarn add @khanacademy/perseus@PR1952 If you are working in Khan Academy's webapp, you can run: ./dev/tools/bump_perseus_version.sh -t PR1952 |
Size Change: +1.71 kB (+0.13%) Total Size: 1.28 MB
ℹ️ View Unchanged
|
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated. # Releases ## @khanacademy/[email protected] ### Minor Changes - [#1738](#1738) [`dbbc82f2d`](dbbc82f) Thanks [@anakaren-rojas](https://github.com/anakaren-rojas)! - add scientific notation button / toggle to basic keypad ## @khanacademy/[email protected] ### Minor Changes - [#1990](#1990) [`37c642f24`](37c642f) Thanks [@SonicScrewdriver](https://github.com/SonicScrewdriver)! - Allow keyboards to navigate and interact with images - [#1738](#1738) [`dbbc82f2d`](dbbc82f) Thanks [@anakaren-rojas](https://github.com/anakaren-rojas)! - add scientific notation button / toggle to basic keypad ### Patch Changes - [#2061](#2061) [`d8b2f7eaf`](d8b2f7e) Thanks [@anakaren-rojas](https://github.com/anakaren-rojas)! - update terminology for angle sides - [#2071](#2071) [`bac10129b`](bac1012) Thanks [@SonicScrewdriver](https://github.com/SonicScrewdriver)! - This patch fixes our Perseus strings to ensure that they are double escaped for Lingui. - [#1952](#1952) [`617377147`](6173771) Thanks [@benchristel](https://github.com/benchristel)! - Internal: add and pass more regression tests for PerseusItem parser - [#2059](#2059) [`53ba9f5d1`](53ba9f5) Thanks [@mark-fitzgerald](https://github.com/mark-fitzgerald)! - [Dropdown] Bugfix - Text in dropdown was shifted up after adding TeX support via Renderer - Updated dependencies \[[`dbbc82f2d`](dbbc82f)]: - @khanacademy/[email protected] ## @khanacademy/[email protected] ### Minor Changes - [#1738](#1738) [`dbbc82f2d`](dbbc82f) Thanks [@anakaren-rojas](https://github.com/anakaren-rojas)! - add scientific notation button / toggle to basic keypad ### Patch Changes - Updated dependencies \[[`d8b2f7eaf`](d8b2f7e), [`bac10129b`](bac1012), [`37c642f24`](37c642f), [`617377147`](6173771), [`dbbc82f2d`](dbbc82f), [`53ba9f5d1`](53ba9f5)]: - @khanacademy/[email protected] - @khanacademy/[email protected] ## @khanacademy/[email protected] ### Patch Changes - Updated dependencies \[[`dbbc82f2d`](dbbc82f)]: - @khanacademy/[email protected]
Issue: LEMS-2582
Test plan:
yarn test